<?php
/*
 *@des 后台基本变量设置
*@author www.speakphp.com
*/
class SysconfigAction extends BaseAction {
	public function __construct(){
		parent::__construct();
		
	}
	public function index(){
		$action_array = array(
				'config','upload'
		);
		$action = $this->_request("action");
		$action = (isset($action) && in_array($action,$action_array))?$action:'config';
		if($action == 'config'){
			$d = D("Admin/Sysconfig");
			$list_data = $d->query_sysconfig(C("web_group"));
			
			$gid = $this->_param("gid" , true );
			
			if(!$gid){
				foreach($list_data as $k => $v ){
					$gid = $k ;
					break;
				}
			}
			$this->assign("group" , C("web_group"));
			$this->assign("list" , $list_data);
			$this->assign("gid" , $gid);
			$this->assign("type" , C("web_type"));
			$this->display();
		}elseif($action == 'upload' ){
			
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize  = 3145728 ;// 设置附件上传大小
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath =  './Data/Upload/Sysconfig/';// 设置附件上传目录
			$upload->autoSub = true;//使用子目录保存上传文件
			$upload->subType = 'date';//使用日期模式创建子目录
			$upload->dateFormat = 'Ymd';//设置子目录日期格式
			if(!$upload->upload()) {// 上传错误提示错误信息
				echo json_encode( array('status' => 0 , 'message'=> $upload->getErrorMsg()));
			
			}else{// 上传成功 获取上传文件信息
				$info =  $upload->getUploadFileInfo();
				
				echo json_encode( array('status' => 1 , 'message'=>'success' , 'info'=>$info[0] ));
			}
		}
		
	}
	//添加系统变量
	public function add(){
		$gid =intval($this->_param("gid"));
		$varname = trim($this->_param("varname" ));//varname
		$value = trim($this->_param("value"));//value
		$info = trim($this->_param("info"  ));//info
		$type = $this->_param("type"  );//type
		$disorder = $this->_param("disorder" );//排序
		if(!array_key_exists($type,C("web_type"))){
			$type = 'string';
		}
		$data = array(
				'varname'=>$varname,
				'value'=>$value,
				'info'=>$info,
				'type'=>$type,
				'groupid'=>$gid,
				'disorder'=>$disorder
		);
		$d = D("Admin/Sysconfig");
		$res = $d->insert_sysconfig($data);
		if($res['status']){
			$this->success("添加成功" , U("sysconfig/index" , array('gid'=>$gid) ));
			exit ; 
		}
		$this->error($res['message'] , U("sysconfig/index" , array('gid'=>980000) ));
	}
	function edit(){
		$gid =intval($this->_GET("gid"));
		$data = $_POST ;
		/* echo "<pre>";
			print_r($data); */
		
		$d = D("Admin/Sysconfig");
		$status = $d->update_data($data);
		if($status){
			$this->success("修改成功" , U("sysconfig/index" , array('gid'=>$gid) ));
		}else{
			$this->error("网络繁忙请稍后！！" , U("sysconfig/index" , array('gid'=>$gid) ));
		}
	}
	//系统基本参数删除
	function del(){
		$gid= $this->_param("gid" );
		$adminGroup = C("super_admin") ;
		if(!in_array($this->username, $adminGroup)){
			$this->error("你没有权限执行此操作" , U("sysconfig/index" , array('gid'=>$gid) ));
			exit;
		}
		$d = D("Admin/Sysconfig");
		$varname = $this->_param("varname"  ) ;
		$varname = str_replace("[]", "", $varname);
	
		$status = $d->del_varname($varname);
		if($status){
			$this->success("删除系统环境变量{$varname}成功",U("sysconfig/index" , array('gid'=>$gid) ));
			exit();
		}
		$this->error("删除系统环境变量{$varname}成功",U("sysconfig/index" , array('gid'=>$gid) ));
		exit();
	}
}
